<template>
  <transition name="slide">
    <music-list :rank="rank" :title="title" :bgImage="bgImage" :songs="songs"></music-list>
  </transition>
</template>

<script>
import MusicList from 'components/music-list/music-list'
import {mapGetters} from 'vuex'
import {GetRankMusic} from 'api/rank'
import {createSong} from 'common/js/song'

export default {
  components:{
    MusicList
  },
  data() {
    return {
      songs: [],
      rank: true
    }
  },
  computed:{
    title(){
      return this.topList.topTitle
    },
    bgImage() {
      if (this.songs.length) {
        return this.songs[0].image
      }
      return ''
    },
    ...mapGetters([
      'topList'
    ])
  },
  created(){
    this.GetRankMusic()
  },
  methods:{
    GetRankMusic(){
      if (!this.topList.id) {
          this.$router.push('/rank')
          return
      }
      GetRankMusic(this.topList.id).then( (res) => {
        if (res.code === 0) {
         this.songs = this._normalizeSongs(res.songlist)
        }
      })
    },
    _normalizeSongs(list) {
      let ret = []
      list.forEach((item) => {
        let musicData = item.data
        if (musicData.songid && musicData.albummid) {
          ret.push(createSong(musicData))
        }
      })
      return ret
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s ease

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>